package com.cskaoyan.search.service.impl;

import com.cskaoyan.mall.commons.constant.SysRetCodeConstants;
import com.cskaoyan.mall.commons.exception.BizException;
import com.cskaoyan.mall.dto.AllItemServiceResponse;
import com.cskaoyan.mall.dto.ItemDto;
import com.cskaoyan.search.consts.SearchRetCode;
import com.cskaoyan.search.converter.ProductConverter;
import com.cskaoyan.search.entity.ItemDocument;
import com.cskaoyan.search.remote.ProductApiClient;
import com.cskaoyan.search.repository.ProductRepository;
import com.cskaoyan.search.service.InitDataService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName InitDataServiceImpl
 * @Description TODO
 * @Author XiaoWJ
 * @Date 2022/9/27 20:18
 **/

@Service
public class InitDataServiceImpl implements InitDataService {

    @Autowired
    ProductApiClient productApiClient;

    @Autowired
    ProductConverter productConverter;

    @Autowired
    ProductRepository productRepository;

    /***
     *  调用shopping服务上传数据库中的数据到es

     */

    @Override
    public void initItems() {
        AllItemServiceResponse allProductItem = productApiClient.getAllProductItem();

        List<ItemDto> itemDtos = allProductItem.getCallData();

        List<ItemDocument> itemDocuments = productConverter.items2Documnets(itemDtos);

        productRepository.saveAll(itemDocuments);

    }
}
